/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.odinms.net.world.guild;

import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

import net.sf.odinms.database.DatabaseConnection;

import net.sf.odinms.client.MapleClient;
import net.sf.odinms.net.MaplePacket;

/**
 *
 * @author Bassoe
 */
public class MapleAlliance implements java.io.Serializable {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MapleAlliance.class);
    private List<MapleGuild> guilds = new ArrayList<MapleGuild>();
    private String rankTitles[] = new String[5];
	private String name;
    private String notice;
	private int id;

    public MapleAlliance(MapleClient c, int idz) {
        guilds = new ArrayList<MapleGuild>();
        this.id = idz;
        notice = loadNotice();
        name = loadName();
        for (int i = 1; i < 6; i++)
            try {
                int meh = loadGuild(i);
                if (meh != 0)
                    if (c != null && meh == c.getPlayer().getGuildId()) {
                        try {
                            guilds.add(c.getChannelServer().getWorldInterface().getGuild(meh, c.getPlayer().getMGC()));
                        } catch (Exception a) {
                            c.getPlayer().dropMessage("fail");
                        }
                    } else {
                        try {
                            guilds.add(new MapleGuild(loadGuild(i)));
                        } catch (Exception e) {
                            c.getPlayer().dropMessage("fail2");
                        }
                    }
                else
                    guilds.add(null);
                rankTitles[i-1] = loadTitles(i);
            } catch (Exception e) {}
    }

    public int loadGuild(int i) {
        Connection con = DatabaseConnection.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement("SELECT guild" + i + " FROM alliances WHERE id = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            int result = 0;
            if (rs.first())
                result = rs.getInt("guild" + i);
            rs.close();
            ps.close();
            return result;
        } catch (Exception ex) {
            log.error("loadGuild error: ", ex);
        }
        return 0;
    }

    public String loadTitles(int i) {
        Connection con = DatabaseConnection.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement("SELECT rank" + i + " FROM alliances WHERE id = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            String result = "";
            if (rs.first())
                result = rs.getString("rank" + i);
            rs.close();
            ps.close();
            return result;
        } catch (Exception ex) {
            log.error("loadGuild error: ", ex);
        }
        return "";
    }

    public String loadNotice() {
        Connection con = DatabaseConnection.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement("SELECT notice FROM alliances WHERE id = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            String result = "";
            if (rs.first())
                result = rs.getString("notice");
            rs.close();
            ps.close();
            return result;
        } catch (Exception ex) {
            log.error("loadGuild error: ", ex);
        }
        return "";
    }

    public String loadName() {
        Connection con = DatabaseConnection.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement("SELECT name FROM alliances WHERE id = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            String result = "";
            if (rs.first())
                result = rs.getString("name");
            rs.close();
            ps.close();
            return result;
        } catch (Exception ex) {
            log.error("loadName error: ", ex);
        }
        return "";
    }

    public void addGuild(MapleClient c, int guildid) {
        for (int i = 0; i < 5; i++)
            if (guilds.get(i) == null) {
                try {
                    guilds.add(c.getChannelServer().getWorldInterface().getGuild(guildid, c.getPlayer().getMGC()));
                } catch (Exception e) {
                    c.getPlayer().dropMessage("Something failed");
                }
                save();
                broadcast(null);
                return;
            }
    }

    public void setTitles(String[] titles) {
        for (int i = 0; i < 5; i++)
            rankTitles[i] = titles[i];
        this.save();
        this.broadcast(null);
    }

    public void broadcast(MaplePacket packet) {
        for (int i = 0; i < 5; i++)
            if (guilds.get(i) != null)
                guilds.get(i).guildMessage(packet);
    }

    public String[] getTitles() {
        return rankTitles;
    }

    public int getId() {
        return id;
    }

    public void setId(int idd) {
        this.id = idd;
    }

    public String getNotice() {
        return notice;
    }

    public void setNotice(String noticee) {
        this.notice = noticee;
        save();
        broadcast(null);
    }

    public String getName() {
        return name;
    }

    public void setName(String namee) {
        this.name = namee;
    }

    public List<MapleGuild> getGuilds() {
        return guilds;
    }

    public void broadcastMessage(MaplePacket packet) {
        for (int i = 0; i < 5; i++)
            if (guilds.get(i) != null)
                guilds.get(i).broadcast(packet);
    }

    public void save() {
        Connection con = DatabaseConnection.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement("UPDATE alliances SET guild1 = ?, guild2 = ?, guild3 = ?, guild4 = ?, guild5 = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ?, notice = ? WHERE id = ?");
            ps.setInt(1, guilds.get(0) != null ? guilds.get(0).getId() : 0);
            ps.setInt(2, guilds.get(1) != null ? guilds.get(1).getId() : 0);
            ps.setInt(3, guilds.get(2) != null ? guilds.get(2).getId() : 0);
            ps.setInt(4, guilds.get(3) != null ? guilds.get(3).getId() : 0);
            ps.setInt(5, guilds.get(4) != null ? guilds.get(4).getId() : 0);
            for (int a = 6; a < 11; a++)
                ps.setString(a , rankTitles[a - 6]);
            ps.setString(11, notice);
            ps.setInt(12, id);
            ps.execute();
            ps.close();
        } catch (SQLException e) {
            log.info("error while saving alliances" + e);
        }
    }

    public int getAmountOfGuilds() {
        int a = 0;
        for (int i = 0; i < 5; i++) {
            if (guilds.get(i) != null) {
                a++;
            }
        }
        return a;
    }
}
